
#-------------------------------------------------------------------------------
# SOLVE THE MODEL UNDER AN ALTERNATIVE CALIBRATION:
# Low and High Persistence for Types
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#Load Functions
#-------------------------------------------------------------------------------
#addprocs(12)
@everywhere begin
     persistence_val   = []
     using Interpolations, Optim, Roots, JLD, Plots
     using Distributions, StatsBase
     include("../../01_Baseline_Model/01_Model_Structure.jl");
     include("../../01_Baseline_Model/02_Functions.jl");
     include("../../01_Baseline_Model/03_Model.jl")
     include("../../01_Baseline_Model/04_Main_Iteration_fx.jl");
     include("../../01_Baseline_Model/05_Load_Solution.jl");
end
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#Solve
#-------------------------------------------------------------------------------

@everywhere begin
NQlow  = 16.0
NQhigh = 48.0
end

for i=1:2
@everywhere   persistence_val = $i

if persistence_val==1
     @printf("*************************** \n")
     @printf("Solving for low persistence \n")
     @printf("*************************** \n")

     #Solve constant type to get an initial guess
     @printf("Solving for constant types \n")
     @printf(" \n")
     @everywhere ce = ce_economy_M(nz=2, NQ=1e10, damp=0.95,  model_start=0, save_start=1,
                                   α= -0.031, β=0.9501, d0=-0.243, d1=0.325,ε=-0.0057,maxiter=1500);
     iterate_VF(ce, tol_max=1e-4)
     #Solve the baseline economy
     @printf(" \n")
     @printf("Solving for baseline \n")
     @everywhere ce = ce_economy_M(model_Chatterjee=1,
                                   α= -0.031, NQ=NQlow, β=0.9501, d0=-0.243, d1=0.325,ε=-0.0057,maxiter=1500 );
     iterate_VF(ce, tol_max=5e-4)
     name_file = string("CE_model_structure_Pers_",ce.NQ,".jld")
     save(string("model_data/",name_file), "ce", ce)

     @printf(" \n")
elseif persistence_val==2
     @printf(" \n")
     @printf("*************************** \n")
     @printf("Solving for high persistence \n")
     @printf("*************************** \n")
     #Solve constant type to get an initial guess
     @printf("Solving for constant types \n")
     @printf(" \n")
     @everywhere ce = ce_economy_M(nz=2, NQ=1e10, damp=0.95,  model_start=0, save_start=1,
                                   α= -0.028, β=0.9505, d0=-0.242, d1=0.325,ε=-0.0044,maxiter=1500 );

     iterate_VF(ce, tol_max=1e-4)

     #Solve the baseline economy
     @printf(" \n")
     @printf("Solving for baseline \n")
     @everywhere ce = ce_economy_M(model_Chatterjee=1, damp=0.98,
                                   α= -0.028, NQ=NQhigh, β=0.9505, d0=-0.242, d1=0.325,ε=-0.0044,maxiter=1500 );
     iterate_VF(ce, tol_max=5e-4, penalty_Π_val=2e-5)
     name_file = string("CE_model_structure_Pers_",ce.NQ,".jld")
     save(string("model_data/",name_file), "ce", ce)
end

end
#-------------------------------------------------------------------------------
